/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui.gui.documents;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.swing.Icon;
import javax.swing.JComponent;

/**
 * 
 * @author stephane
 */
public abstract class AbstractDocument extends JComponent implements Cloneable {
	private static final long serialVersionUID = 1L;

	private File _file = null;

	private String _extension = "";

	private String _fileDescription = "";

	private String _filterPath = "";

	private List<EditorAction> _actions = new ArrayList<EditorAction>();

	private Node _node = null;

	protected DockableDocument _parent = null;

	private boolean _dirty = false;


	public boolean isDirty() {
		return this._dirty;
	}

	public void setIsDirty(boolean value) {
		this._dirty = value;
			if (this._dirty) {
				if (getFileName() != null && getFileName().trim().length() > 0) {
					_parent.changeTitle(getFileName() + "*");
				} else {
					_parent.changeTitle("newDocument." + getExtension() + "*");
				}
			} else {
				if (getFileName() != null && getFileName().trim().length() > 0) {
					_parent.changeTitle(getFileName());
				}
			}
			
	}

	public void setParent(DockableDocument parent) {
		this._parent = parent;
	}

	public AbstractDocument() {
		this._node = new Node(this, this);

	}

	protected final void addAction(EditorAction action) {
		this._actions.add(action);
	}

	public boolean isTitleShown() {
		return true;
	}

	public Node getNode() {
		return this._node;
	}

	/**
	 * @return the _fileName
	 */
	public String getFileName() {
		if (_file== null && _parent != null) {
			_parent.changeTitle("(not saved)");
		}
		return _file != null ? _file.getName() : "";
	}

	public abstract void initComponents();

	/**
	 * @param fileName
	 *            the _fileName to set
	 */
	public final void setFile(File fileName) {
		this._file = fileName;
		initComponents();
	}

	public File getFile() {
		return this._file;
	}

	/**
	 * @return the _extension
	 */
	public String getExtension() {
		return _extension;
	}

	/**
	 * @param extension
	 *            the _extension to set
	 */
	protected void setExtension(String extension) {
		this._extension = extension;
	}

	/**
	 * @return the _filterPath
	 */
	public String getFilterPath() {
		return _filterPath;
	}

	/**
	 * @param filterPath
	 *            the _filterPath to set
	 */
	public void setFilterPath(String filterPath) {
		this._filterPath = filterPath;
	}

	public abstract void OnLoad() throws Exception;

	public abstract void OnSave() throws Exception;

	public void OnNew() {
	}

	public abstract Icon getIcon();

	protected void clearAvailableAction() {
		_actions.clear();
	}

	public List<EditorAction> getAvailableActions() {
		return Collections.unmodifiableList(_actions);
	}

	public void refresh() {

	}

	/**
	 * @param _fileDescription
	 *            the _fileDescription to set
	 */
	protected void setFileDescription(String _fileDescription) {
		this._fileDescription = _fileDescription;
	}

	public DockableDocument getDockParent() {
		return this._parent;
	}
	/**
	 * @return the _fileDescription
	 */
	public String getFileDescription() {
		return _fileDescription;
	}

	public Object clone() {
		try {
			AbstractDocument doc = this.getClass().newInstance();
			return doc;
		} catch (Exception ex) {

		}
		return null;
	}
}
